<?php

namespace App\Http\Controllers\Admin;

use App\Models\Shop;
use App\Models\Worker;

/**
 * 工人管理控制器
 */
class WorkerController extends BaseController
{

    public function __construct(Shop $shop,Worker $worker)
    {
        parent::__construct();
        $this->shop = $shop;
        $this->worker = $worker;
    }


    public function getIndex()
    {
//        $this->worker->checkMaxOrderNum(1);
        $this->worker->_redisChangeMaxOrderNum(1,-1);
        $shopList = $this->shop->_getList(['admin_id' => is_login()])->toArray();
        $rows = $this->worker->getWorkerList(request('shop_id'));
        if (request('shop_id')) {
            foreach ($shopList as $v) {
                if ($v['id'] == request('shop_id')) {
                    $curentShop = $v;
                }
            }
        }
        return view('admin.worker.index', compact('rows', 'shopList', 'curentShop'));
    }

    public function getShopIndex()
    {
        $shopList = $this->shop->_getList(['admin_id' => is_login()])->toArray();
        $rows = $this->worker->getOwnWorkerList(request('shop_id'));
        if (request('shop_id')) {
            foreach ($shopList as $v) {
                if ($v['id'] == request('shop_id')) {
                    $curentShop = $v;
                }
            }
        }
        return view('admin.worker.index', compact('rows', 'shopList', 'curentShop'));
    }

    public function getInfo()
    {
        $info = $this->worker->_getOneById(request('id'));
        return view('admin.worker.info', compact('info'));
    }

    public function getAdd()
    {
        return view('admin.worker.edit');
    }

    public function postAdd()
    {
        $res = $this->worker->addOrUpdate(request('shop_id'));
        if (!$res) {
            return $this->error($this->worker->getError() ? $this->worker->getError() : '未知错误');
        }
        if(request('shop_id')) {
            return $this->success('新增成功', urlAdmin('worker', 'getShopIndex', ['shop_id' => request('shop_id')]));
        }
        return $this->success('新增成功', urlAdmin('worker', 'getIndex'));
    }

    public function getEdit()
    {
        $info = $this->worker->_getOneById(request('id'));
        $info = $info ? $info->toArray() : [];
        return view('admin.worker.edit', compact('info'));
    }

    public function postEdit()
    {
        $res = $this->worker->addOrUpdate(request('shop_id'));
        if (!$res) {
            return $this->error($this->worker->getError() ? $this->worker->getError() : '未知错误');
        }
        if(request('shop_id')) {
            return $this->success('更新成功', urlAdmin('worker', 'getShopIndex', ['shop_id' => request('shop_id')]));
        }
        return $this->success('更新成功', urlAdmin('worker', 'getIndex'));
    }

    public function getRemoveBindWx()
    {
        $res = $this->worker->removeBindWx(request('id'));
        if (!$res) {
            return $this->error($this->worker->getError() ? $this->worker->getError() : '未知错误');
        }
        return $this->success($res);
    }

    public function anySetStatus()
    {
        $ids = is_array(request('ids')) ? request('ids') : [request('ids')];
        $status = request('status');
        if (empty($ids)) {
            return $this->error('请选择要操作的数据');
        }
        $where['id'] = ['in', $ids];
        switch ($status) {
            case 0  :
                $this->worker->_redisGenerateByIds($ids);
                return $this->forbid($this->worker, $where, ['success' => '禁用成功', 'error' => '禁用失败']);
            case 1  :
                $this->worker->_redisGenerateByIds($ids);
                return $this->resume($this->worker, $where, ['success' => '启用成功', 'error' => '启用失败']);
            default :
                return $this->error('参数错误');
        }
    }
}
